package com.shark.uh.controller;


import com.shark.uh.common.ApiResponse;
import com.shark.uh.model.dto.roles.RoleDTO;
import com.shark.uh.model.dto.roles.RoleRemoveDTO;
import com.shark.uh.model.dto.roles.RoleSetDTO;
import com.shark.uh.model.vo.RoleCreatorVO;
import com.shark.uh.service.RolesService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;

@Validated
@RestController()
public class RoleController {

    @Resource
    private RolesService rolesService;


    /**
     * 获取角色创建者列表
     *
     * @return
     */
    @GetMapping("api/role/creator-list")
    public ApiResponse<List<RoleCreatorVO>> creatorList() {
        List<RoleCreatorVO> creators = rolesService.getCreators();
        return ApiResponse.success(creators);
    }


    /**
     * 条件查询角色列表
     *
     * @param roleName      角色名称
     * @param creatorUserId 创建者ID
     * @param status        角色状态
     * @param sortBy        排序方式
     * @param pageNo        页号
     * @param pageSize      条数
     * @return 角色列表
     */
    @GetMapping("api/role/list")
    public ApiResponse<List<RoleDTO>> list(@RequestParam(required = false) String roleName,
                                           @RequestParam(required = false) String creatorUserId,
                                           @RequestParam(required = false) String status,
                                           @RequestParam(required = false) String sortBy,
                                           @RequestParam() @Valid @Min(1) Integer pageNo,
                                           @RequestParam() @Valid @Min(10) @Max(100) Integer pageSize) {

        List<RoleDTO> roleDTOS = rolesService.listConditional(roleName, creatorUserId, status, pageNo, pageSize, sortBy);
        return ApiResponse.success(roleDTOS);
    }


    /**
     * 保存角色
     *
     * @param roleName    角色名称
     * @param description 描述
     * @return ApiResponse
     */
    @PostMapping("api/role/save")
    public ApiResponse addRole(@RequestParam @NotBlank @Size(min = 1, max = 50) String roleName,
                               @RequestParam @NotBlank @Size(min = 1, max = 255) String description) {
        rolesService.save(roleName, description);
        return ApiResponse.success();
    }


    /**
     * 更新角色信息
     *
     * @param roleId         角色ID
     * @param newDescription 新描述
     * @return ApiResponse
     */
    @PutMapping("api/role/update")
    public ApiResponse updateRole(@RequestParam("roleId") @NotBlank String roleId,
                                  @RequestParam("newDescription") @NotBlank @Size(min = 1, max = 255) String newDescription) {
        rolesService.update(roleId, newDescription);
        return ApiResponse.success();
    }


    /**
     * 删除角色
     *
     * @param roleId 角色ID
     * @return ApiResponse
     */
    @DeleteMapping("api/role/delete/{roleId}")
    public ApiResponse deleteRole(@PathVariable("roleId") @NotBlank String roleId) {
        rolesService.remove(Long.parseLong(roleId));
        return ApiResponse.success();
    }

//    @PostMapping("api/role/search")
//    public ApiResponse<RolesResponseDTO> searchRoles(@RequestBody RoleQuery roleQuery){
//        RolesResponseDTO rolesResponseDTO = rolesService.listRoles(roleQuery);
//        return ApiResponse.success(rolesResponseDTO);
//    }
//
//    @GetMapping("api/role/users/{roleId}")
//    public ApiResponse<UsersResponseDTO> getUsersByRoleId(@PathVariable("roleId") Long roleId){
//        UsersResponseDTO usersResponseDTO = rolesService.listUsersByRoleId(roleId);
//        return ApiResponse.success(usersResponseDTO);
//    }
//
//    @GetMapping("api/user-roles/{userId}")
//    public ApiResponse<RolesResponseDTO> getRolesByUserId(@PathVariable("userId") Long userId){
//        RolesResponseDTO rolesResponseDTO = rolesService.listRolesByUserId(userId);
//        return ApiResponse.success(rolesResponseDTO);
//    }

    @PostMapping("api/role/set/user")
    public ApiResponse setRolesForUser(@RequestBody RoleSetDTO roleSetDTO) {
        rolesService.addRoleForUser(roleSetDTO);
        return ApiResponse.success();
    }

    @PostMapping("api/role/remove/user")
    public ApiResponse removeRolesFromUser(@RequestBody RoleRemoveDTO roleRemoveDTO) {
        rolesService.deleteRoleFromUser(roleRemoveDTO);
        return ApiResponse.success();
    }

    @PostMapping("api/role/disable")
    public ApiResponse disableRoles(@RequestBody List<Long> roleIds) {
        rolesService.banRole(roleIds);
        return ApiResponse.success();
    }

    @PostMapping("api/role/enable")
    public ApiResponse enableRoles(@RequestBody List<Long> roleIds) {
        rolesService.unbanRole(roleIds);
        return ApiResponse.success();
    }


}
